package com.pig4cloud.pig.homecenter.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.common.security.service.PigUser;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.homecenter.entity.HassAreaRegistryEntity;
import com.pig4cloud.pig.homecenter.entity.HassDeviceRegistryEntity;
import com.pig4cloud.pig.homecenter.enums.TrueFalse;
import com.pig4cloud.pig.homecenter.mapper.HassDeviceRegistryMapper;
import com.pig4cloud.pig.homecenter.service.HassDeviceRegistryService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备数据
 *
 * @author pig
 * @date 2025-01-07 11:48:58
 */
@Service
public class HassDeviceRegistryServiceImpl extends ServiceImpl<HassDeviceRegistryMapper, HassDeviceRegistryEntity> implements HassDeviceRegistryService {

	@Override
	public void syncHassDeviceRegistry(List<HassDeviceRegistryEntity> deviceRegistryList) {
		PigUser user = SecurityUtils.getUser();
		deviceRegistryList.forEach(entity -> {
			entity.setUserId(user.getId());
			QueryWrapper<HassDeviceRegistryEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().eq(HassDeviceRegistryEntity::getUserId, entity.getUserId())
					.eq(HassDeviceRegistryEntity::getId, entity.getId());
			HassDeviceRegistryEntity curDevice = getOne(queryWrapper, false);
			if (curDevice != null) {
				curDevice.setName(entity.getName());
				curDevice.setModifiedAt(entity.getModifiedAt());
				curDevice.setDisabledBy(entity.getDisabledBy());
				curDevice.setNameByUser(entity.getNameByUser());
				curDevice.setPrimaryConfigEntry(entity.getPrimaryConfigEntry());
				curDevice.setSwVersion(entity.getSwVersion());
				curDevice.setHwVersion(entity.getHwVersion());

				updateById(curDevice);
			}else {
				entity.setCustomName(entity.getName());
				entity.setVisible(true);
				save(entity);
			}
		});
	}
}
